using Nirvana;
using System.Collections;
using System.Collections.Generic;
using Unity.Burst.CompilerServices;
using UnityEngine;

public class testPoissonDiskSampling : MonoBehaviour
{
    public float radius;
    public Vector2 sampleRegionSize;
    public int avgNum;
    public bool isRandomRadius;
    public Vector2 randomRadius;
    private List<Vector3> _listpos = new List<Vector3>();

    private List<PoissonDiskSampling.PoissonDiskSamplingData> _listpos2 = new List<PoissonDiskSampling.PoissonDiskSamplingData>();

    private void OnEnable()
    {
        _listpos2.Clear();
        _listpos.Clear();
        if (isRandomRadius )
        {

            _listpos2 = PoissonDiskSampling.GenerateRandomRaduisPoints(randomRadius, sampleRegionSize, avgNum);
            Debug.Log("point2 count : " + _listpos2.Count);
        }
        else
        {
            
            _listpos = PoissonDiskSampling.GeneratePoints(radius, sampleRegionSize, avgNum);
            Debug.Log("point count : " + _listpos.Count);
        }

    }


    private void OnDrawGizmos()
    {
        Gizmos.color = Color.red;
        float x0 = transform.position.x;
        float z0 = transform.position.z;

        Vector3 v0 = new Vector3(x0, 0, z0);
        Vector3 v1 = new Vector3(x0, 0, z0 + sampleRegionSize.y);



        Gizmos.color = new Color(1, 1, 1, 0.5f);
       
        float cellSize = radius / Mathf.Sqrt(2);
        int[,] grid = new int[Mathf.CeilToInt(sampleRegionSize.x / cellSize), Mathf.CeilToInt(sampleRegionSize.y / cellSize)];
        int xLen = grid.GetLength(0);
        int yLen = grid.GetLength(1);

        for (int i = 0; i < xLen + 1; i++)
        {

            v1 = new Vector3(i * cellSize + x0, 0, z0 + sampleRegionSize.y + cellSize);
            v0 = new Vector3(i * cellSize + x0, 0, z0);
            Gizmos.DrawLine(v0, v1);
        }
        for (int j = 0; j < yLen + 1; j++)
        {

            v0 = new Vector3(x0, 0, z0 + j * cellSize);
            v1 = new Vector3(x0 + sampleRegionSize.x + cellSize, 0, z0 + j * cellSize);
            Gizmos.DrawLine(v0, v1);
        }
        Gizmos.color = Color.green;
        if (!isRandomRadius)
        {          
            for (int i = 0; i < _listpos.Count; i++)
            {
                var pos = _listpos[i] + transform.position;
                Gizmos.DrawWireSphere(pos, radius);
            }
        }
        else
        {
            for (int i = 0; i < _listpos2.Count; i++)
            {
                var pos = _listpos2[i].pos + transform.position;
                Gizmos.DrawWireSphere(pos, _listpos2[i].raduis);
            }


        }



    }

}
